﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.BLL
{
    partial class BizBienCheSinhVienVaoHocKy
    {
        /// <summary>
        /// Biên chế sinh viên vào một học kỳ kèm học phí/lệ phí
        /// </summary>
        /// <param name="maSinhVien"></param>
        /// <param name="maHocKy"></param>
        /// <param name="leHocPhi"></param>
        /// <param name="laHocPhi"></param>
        /// <returns></returns>
        internal static BienCheSinhVienVaoHocKy BienCheSinhVien(string maSinhVien, string maHocKy, int leHocPhi, bool laHocPhi = true)
        {
            using (var context = new QLHPEntities())
            {
                var bienChe = (from e in context.BienCheSinhVienVaoHocKiDbSet
                               where
                               (e.MaSinhVien == maSinhVien) &&
                               (e.MaHocKy == maHocKy)
                               select e).SingleOrDefault();
                if (bienChe == null)
                {
                    if (laHocPhi)
                        bienChe = new BienCheSinhVienVaoHocKy()
                        {
                            MaSinhVien = maSinhVien,
                            MaHocKy = maHocKy,
                            HocPhi = leHocPhi,
                            MaBienChe = Guid.NewGuid().ToString()
                        };
                    else
                        bienChe = new BienCheSinhVienVaoHocKy()
                        {
                            MaSinhVien = maSinhVien,
                            MaHocKy = maHocKy,
                            LePhi = leHocPhi,
                            MaBienChe = Guid.NewGuid().ToString()
                        };
                    context.BienCheSinhVienVaoHocKiDbSet.Add(bienChe);
                    context.SaveChanges();
                }
                else
                {
                    if (laHocPhi)
                        bienChe.HocPhi = leHocPhi;
                    else
                        bienChe.LePhi = leHocPhi;
                    context.SaveChanges();
                }
                return bienChe;
            }
        }
    }
}
